package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.NoticeMapper;
import com.zretc.entity.Notice;
import com.zretc.entity.ResponseResult;
import com.zretc.service.NoticeService;
import com.zretc.util.PageMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public PageInfo<Notice> getAll(Map<String, String> condition) {
        String title = condition.get("title");
        if(title ==null || "".equals(title)){
            condition.put("title",null);
        }else{
            condition.put("title","%"+title+"%");
        }
        PageHelper.startPage(PageMapUtil.pageNumber(condition),PageMapUtil.pageSize(    condition));
        List<Notice> all = noticeMapper.getAll(condition);
        PageInfo<Notice> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }

    @Override
    public Notice getOne(Integer noticeId) {
        return noticeMapper.getOne(noticeId);
    }

    @Override
    public ResponseResult insert(Notice notice) {
        if(noticeMapper.insert(notice)== 1){
            return ResponseResult.ok("发布成功");
        }
        return ResponseResult.error("发布失败");
    }

    @Override
    public ResponseResult update(Notice notice) {
        if(noticeMapper.update(notice)== 1){
            return ResponseResult.ok("更新成功");
        }
        return ResponseResult.error("更新失败");
    }

    @Override
    public ResponseResult delete(Integer noticeId) {
        if(noticeMapper.delete(noticeId)== 1){
            return ResponseResult.ok("删除成功");
        }
        return ResponseResult.error("删除失败");
    }

    @Override
    public Notice getNew() {
        return noticeMapper.getNew();
    }


}
